<include file="Public/min-header"/>
<div class="wrapper">
	<include file="Public/breadcrumb"/>
	<section class="content">
		<div>
			<div class="col-xs-12">
				<div class="box">
					<div class="box-header">
						<nav class="navbar" style="margin-bottom: 0; display: flex; align-items: center;">
							<a href="javascript:;" onclick="go_class_info(this)" data-url="{:U('Store/goods_class_info')}" class="btn btn-primary pull-right"><i class="fa fa-plus"></i> 新增分类</a>
						</nav>
					</div><!-- /.box-header -->
					<div class="box-body">
						<div class="row">
							<div class="col-sm-12">
								<table id="list-table" class="table table-bordered table-striped">
									<thead>
									<tr role="row">
										<th width="80"><label style="display: flex; align-items: center; margin-bottom: 0; cursor: pointer;">
											<input type="checkbox" style="margin-top: 0; margin-right: 5px;" onclick="javascript:$('input[name*=\'category\']').prop('checked', this.checked);">全选</label>
										</th>
										<th>分类id</th>
										<th>图标</th>
										<th>分类名称</th>
										<th>是否导航显示</th>
										<th>排序</th>
										<th>操作</th>
									</tr>
									</thead>
									<tbody>
									<foreach name="cat_list" item="vo" key="k" >
										<tr role="row" align="left" class="parent-tr" data-cat-id="{$vo.cat_id}" data-is-parent="{$vo[deep]|default=1}">
											<td><input type="checkbox" name="category[]" value="{$vo.cat_id}">&nbsp;&nbsp;<if condition="$vo[deep] eq 2">&nbsp;&nbsp;&nbsp;&nbsp;</if></td>
											<td>
												{$vo.cat_id}
											</td>
											<td>
												<img alt="" src="{$vo.image}" width="80px" height="50px">
												商品数量：{$vo.count}
											</td>
											<td>
												<if condition="$vo[deep] neq 1">
													--
												</if>
												{$vo.cat_name}
												<if condition="$vo[deep] eq 1">
													<a class="btn btn-default" data-url="{:U('Store/goods_class_info',array('parent_id'=>$vo['cat_id']))}" href="javascript:;" onclick="go_class_info(this)">新增下级</a>
													<span class="toggle-children" style="margin-left: 10px; cursor: pointer; color: #0073b7; font-weight: bold;">[+]</span>
													<else/>
													上级：{$vo.parent_name}
												</if>
											</td>
											<td>
												<if condition="$vo[is_nav_show] eq 1"> 是     <else /> 否</if>
											</td>
											<td>
												<input type="text"  class="input-sm" onkeyup="this.value=this.value.replace(/[^\d]/g,'')" onpaste="this.value=this.value.replace(/[^\d]/g,'')" onchange="updateSort2('store_goods_class','cat_id','{$vo.cat_id}','cat_sort',this)" size="4" value="{$vo.cat_sort}" />
											</td>
											<td>
												<a class="btn btn-primary" data-url="{:U('Store/goods_class_info',array('cat_id'=>$vo['cat_id']))}" href="javascript:;" onclick="go_class_info(this)"><i class="fa fa-pencil"> 修改</i></a>
												<a class="btn btn-danger" href="javascript:void(0)" data-url="{:U('Store/goods_class_save')}" data-id="{$vo.cat_id}" onclick="delfun(this)"><i class="fa fa-trash-o"></i></a>
											</td>
										</tr>
									</foreach>
									</tbody>
								</table></div>
						</div>
					</div>
				</div>
			</div>
		</div>
	</section>
</div>
<script type="text/javascript">
	function delfun(obj){
		if(confirm('确认删除')){
			$.ajax({
				type : 'post',
				url : $(obj).attr('data-url'),
				data : {act:'del',cat_id:$(obj).attr('data-id')},
				dataType : 'json',
				success : function(data){
					if(data.stat=='ok'){
						$(obj).parent().parent().remove();
						// 删除子分类行
						var parentId = $(obj).closest('tr').data('cat-id');
						$('tr.child-tr[data-parent-id="' + parentId + '"]').remove();
					}else{
						layer.alert(data, {icon: 2});
					}
				}
			})
		}
		return false;
	}

	// 修改指定表的排序字段 给商家使用的函数
	function updateSort2(table,id_name,id_value,field,obj)
	{
		var value = $(obj).val();
		if(field=="price" && (value=="" || value=="0")){
			alert("价格不能为：0 ！");
			return false;
		}
		$.ajax({
			url:"/index.php?m=Seller&c=Index&a=changeTableVala&table="+table+"&id_name="+id_name+"&id_value="+id_value+"&field="+field+'&value='+value,
			success: function(data){
				layer.msg('更新成功', {icon: 1});
			}
		});
	}
	function go_class_info(o){
		var url = $(o).attr('data-url');
		layer.open({
			type: 2,
			title: '编辑分类',
			shadeClose: true,
			shade: 0.2,
			area: ['60%', '90%'],
			content: url,
		});
	}

	//回调函数
	function call_back(msg){
		if(msg>0){
			layer.msg('操作成功', {icon: 1});
			layer.closeAll('iframe');
			window.location.reload();
		}else{
			layer.msg('操作失败', {icon: 3});
			layer.closeAll('iframe');
		}
	}

	// 页面加载完成后执行
	$(document).ready(function() {
		// 隐藏所有二级分类
		$('tr[data-is-parent="2"]').hide().addClass('child-tr');

		// 为每个一级分类添加子分类关联
		$('tr[data-is-parent="1"]').each(function() {
			var parentId = $(this).data('cat-id');
			$(this).nextUntil('tr[data-is-parent="1"]', 'tr[data-is-parent="2"]').addClass('child-tr').attr('data-parent-id', parentId);
		});

		// 点击[+]或[-]切换子分类显示/隐藏
		$('.toggle-children').click(function(e) {
			e.stopPropagation();
			var $parentRow = $(this).closest('tr');
			var parentId = $parentRow.data('cat-id');
			var $childRows = $('tr.child-tr[data-parent-id="' + parentId + '"]');
			var isHidden = $childRows.first().is(':hidden');

			$childRows.toggle();

			// 更新展开/折叠图标
			$(this).text(isHidden ? '[-]' : '[+]');
		});

		// 确保所有一级分类的展开图标正确
		$('tr[data-is-parent="1"]').each(function() {
			var parentId = $(this).data('cat-id');
			var $childRows = $('tr.child-tr[data-parent-id="' + parentId + '"]');
			var $toggle = $(this).find('.toggle-children');
			if ($childRows.length > 0) {
				if ($childRows.first().is(':visible')) {
					$toggle.text('[-]');
				} else {
					$toggle.text('[+]');
				}
			} else {
				$toggle.remove(); // 没有子分类，移除图标
			}
		});
	});
</script>
</body>
</html>